package org.wattdepot.cli.command;

import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.processor.CommandLineInterface;
import org.wattdepot.client.WattDepotClient;

/**
 * Holds a method to do the carboncontent command which checks carbon content for a timestamp date
 * for WattDepot.
 * 
 * @author Kendyll Doi, Kimberly Heu
 */
public class WattCarbonContent {
  /**
   * Finds the carbon generated per MWh from a source given a timestamp and interval.
   * 
   * @param source a location to analyze
   * @param time the time at which you wish to draw data
   * @param interval the minutes in which you observe change in data
   * @param client the client which contains the data
   * @return the results from the calculations
   */
  public static String wattCarbonContent(String source, String time, int interval,
      WattDepotClient client) {
    String output = null;
    String carbon = null;
    String energy = null;
    double content = 0.0;
    if (interval <= 0) {
      return "interval must be greater than 0";
    }
    try {

      XMLGregorianCalendar timestamp = CommandLineInterface.createTstamp(time);
      if (timestamp == null) {
        return "Time argument not correct";
      }
      carbon =
          WattTotalGenerated.totalGenerated("carbon", source, time, String.valueOf(interval),
              client);
      energy =
          WattTotalGenerated.totalGenerated("energy", source, time, String.valueOf(interval),
              client);
      content =
          Double.parseDouble(carbon.substring(17))
              / (Double.parseDouble(energy.substring(17)) / 1000000);
      output = content + "lbs CO2/MWh";
      return output;
    }
    catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      output = "Error";
    }
    return output;
  }
}
